package com.kaesun.ufit.weight;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.kaesun.ufit.hardware.Buzzer;
import com.kaesun.ufit.tools.ConfigData;
import com.kaesun.ufit.tools.ToolbarView;

public class WarningActivity extends Activity implements View.OnClickListener, IOnHandlerMessage {
	/*private static final String TAG = "WarningActivity";
	private void log(String log) {
		Log.i(TAG, log);
	}*/

	private static final int BUTTON_IDS[] = {
		R.id.button_back, R.id.button_warning, R.id.button_guide
	};

	private MainApplication mApp = null;
	private WeakRefHandler mHandler = null;
	private ToolbarView mToolbarView = null;

	private long mElapsedTime = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_warning);

		for (int i=0; i<BUTTON_IDS.length; i++) {
			findViewById(BUTTON_IDS[i]).setOnClickListener(this);
		}

		mApp = (MainApplication)getApplication();
		mHandler = new WeakRefHandler((IOnHandlerMessage)this);
		mToolbarView = (ToolbarView)findViewById(R.id.view_toolbar);

		mToolbarView.setTitle("Warning");
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	@Override
	protected void onPause() {

		super.onPause();
	} 

	@Override
	protected void onResume() {
		mApp.setHandler(mHandler, true);
		mElapsedTime = mApp.rtcGetTimeInMillis();

		checkLanguage();

		super.onResume();
	}

	public void onClick(View arg0) {
		mApp.timeoutReset();

		if (1000 > (mApp.rtcGetTimeInMillis() - mElapsedTime)) {
			return;
		}
		mElapsedTime = mApp.rtcGetTimeInMillis();

		Buzzer.delay(50);
		switch (arg0.getId()) {
		case R.id.button_back:
			setResult(RESULT_OK);
			finish();
			break;
		case R.id.button_warning:
			break;
		case R.id.button_guide:
			setResult(RESULT_CANCELED);
			finish();
			break;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mApp.timeoutReset();

		return super.onTouchEvent(event);
	}

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case MainApplication.HANDLER_TIMEOUT_SECOND:
			mToolbarView.setTime(msg.arg1, msg.arg2);
			break;
		case MainApplication.HANDLER_TIMEOUT_ACTIVITY:
			setResult(RESULT_OK);
			finish();
			break;
		}
	}

	private int _serviceLanguage = ConfigData.DEF_SERVICE_LANGUAGE;
	private void checkLanguage() {
		int language = ConfigData.getPrefInteger(this, ConfigData.PREF_SERVICE_LANGUAGE, ConfigData.DEF_SERVICE_LANGUAGE);
		if (_serviceLanguage == language) {
			return;
		}
		_serviceLanguage = language;

		int imgId = 0;
		switch (_serviceLanguage) {
		case ConfigData.SERVICE_LANGUAGE_ENG:
			imgId = R.drawable.img_warning_eng;
			break;
		case ConfigData.SERVICE_LANGUAGE_KOR:
		default:
			imgId = R.drawable.img_warning;
			break;
		}
		if (0 < imgId) {
			((ImageView)findViewById(R.id.image_warning)).setImageResource(imgId);
		}
	}
}
